import 'package:flutter/material.dart';

///
/// 确认对话框，可实现一个按钮，也可以实现两个按钮
///
class QConfirmAlert extends Dialog {
  final BuildContext buildContext;
  final List<String> buttonItems;
  final String title;
  final String content;
  final Function? onTap;

  const QConfirmAlert({
    Key? key,
    required this.buildContext,
    required this.buttonItems,
    this.title = "",
    this.content = "请您确认信息!",
    this.onTap,
  })  : assert(buttonItems.length > 0 && buttonItems.length <= 2),
        super(key: key);

  /// 取消对话框
  static hide(BuildContext context) {
    Navigator.of(context).pop();
  }

  /// 显示对话框
  void show() {
    showDialog(
      context: buildContext,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return _ConfirmContent(
          title: title,
          content: content,
          buttonItems: buttonItems,
          onTap: onTap,
        );
      },
    );
  }
}

class _ConfirmContent extends StatefulWidget {
  final List<String> buttonItems;
  final String? title;
  final String? content;
  final Function? onTap;

  const _ConfirmContent({
    Key? key,
    required this.buttonItems,
    this.title,
    this.content,
    this.onTap,
  }) : super(key: key);

  @override
  _ConfirmContentState createState() => _ConfirmContentState();
}

class _ConfirmContentState extends State<_ConfirmContent> {
  /// 拦截返回键
  Future<bool> _requestPop() {
    return Future.value(false);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
      onWillPop: _requestPop,
      child: Material(
        color: Colors.transparent,
        child: Center(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: Container(
              color: Colors.white,
              width: 566 / 2,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  widget.title != ''
                      ? Container(
                          margin: const EdgeInsets.only(bottom: 16, top: 16),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                widget.title!,
                                style: const TextStyle(
                                    fontSize: 18, fontWeight: FontWeight.w600),
                              ),
                            ],
                          ),
                        )
                      : const SizedBox(
                          height: 20,
                        ),
                  Container(
                    padding: const EdgeInsets.only(left: 20, right: 20),
                    child: Text(
                      widget.content!,
                      textAlign: TextAlign.center,
                      style: const TextStyle(fontSize: 16),
                    ),
                  ),
                  const SizedBox(height: 30),
                  const Divider(
                    height: 0.5,
                  ),
                  _getButtons()
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _getButtons() {
    return Row(
        children: widget.buttonItems.map(
      (res) {
        int index = widget.buttonItems.indexOf(res);
        return Expanded(
          child: GestureDetector(
            onTap: () {
              Navigator.pop(context);
              if (widget.onTap != null) {
                widget.onTap!(index);
              }
            },
            child: Container(
              alignment: Alignment.center,
              height: 45,
              decoration: BoxDecoration(
                border: Border(
                  right: BorderSide(
                      width: 0.5,
                      color: widget.buttonItems.length == 2
                          ? const Color(0xFFDDDDDD)
                          : Colors.white),
                ),
              ),
              child: Text(
                res,
                style: TextStyle(
                  fontSize: 18,
                  color: widget.buttonItems.length == 2 && index == 1
                      ? const Color(0xFF177FF3)
                      : const Color(0xFF606972),
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        );
      },
    ).toList());
  }
}
